<?php
/**
 * Created by PhpStorm.
 * User: aeball
 * Date: 2017/4/28
 * Time: 16:01
 */

namespace app\index\controller;

use app\common\model\User as UserModel;
use think\Config;
use think\Controller;
use think\Session;
use think\Db;

/*
 * 前台登录
 * */
class Regist  extends Controller
{
    protected $user_model;

    protected function _initialize(){
        parent::_initialize();
        $this->user_model = new UserModel;
    }

    public function index(){
        return $this->fetch();
    }

    /*
     * 账户注册手机号验证 code
     * */
    public function regist_checkphone(){
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'User');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $checkStr = $data['mobile'].','.$data['verify'];
                $checkVerify = checode($checkStr);
                if($checkVerify){
                    $data['header_url'] = '/static/images/image_about.jpg';
                    $data['nickname'] = 'sc_'.substr(md5(time()),5,7);
                    $data['password'] = md5($data['password'] . Config::get('salt'));
                    if ($this->user_model->allowField(true)->save($data)) {
                        return json(['status'=>1,'msg'=>'注册成功']);
                    } else {
                        return json(['status'=>0,'msg'=>'注册失败']);
                    }
                }else{
                    return false;
                }
            }
        }
    }

    /*
     * 账户注册手机号不验证
     * */
    public function regist(){
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            /*
             * 校验用户名是否重复
             * */
            $is_repeat = $this->user_model->where(['username'=>['eq',$data['username']]])->select();
            if($is_repeat){
                $this->error('用户名已存在', 'index/index/index');
//                return json(['status'=>0,  'msg'=>'用户名已存在']);
            }else{
                $data['header_url'] = '/static/images/image_about.jpg';
                $data['nickname'] = 'sc_'.substr(md5(time()),5,7);
                $data['password'] = md5($data['password'] . Config::get('salt'));
                if ($this->user_model->allowField(true)->save($data)) {
                    // 注册成功, 获取服务器信息登录
            //    $admin_user = Db::name('user')->field('id,username,status,create_time,sex,mobile')->where($data)->find();
            //    Session::set('index_user', $admin_user);
            //    Session::set('index_id', $admin_user['id']);
            //    Session::set('index_name', $data['username']);
                    $this->success('注册成功', 'index/index/index');
//                    return json(['status'=>1,  'msg'=>'注册成功']);
                } else {
                    $this->error('注册失败', 'index/index/index');
//                    return json(['status'=>0,'msg'=>'注册失败']);
                }
            }
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        Session::delete('index_id');
        Session::delete('index_name');
        $this->success('退出成功', 'index/index/index');
    }






}